home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2000 March / maximum-cd-2000-03.iso / Quake3 Game Source / Q3AGameSource.exe / Main / cg_snapshot.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-01-18  |  9.8 KB  |  372 lines

  1. // Copyright (C) 1999-2000 Id Software, Inc.
  2. //
  3. // cg_snapshot.c -- things that happen on snapshot transition,
  4. // not necessarily every single rendered frame
  5.  
  6. #include "cg_local.h"
  7.  
  8.  
  9.  
  10. /*
  11. ==================
  12. CG_ResetEntity
  13. ==================
  14. */
  15. static void CG_ResetEntity( centity_t *cent ) {
  16.     // if an event is set, assume it is new enough to use
  17.     // if the event had timed out, it would have been cleared
  18.     cent->previousEvent = 0;
  19.  
  20.     cent->trailTime = cg.snap->serverTime;
  21.  
  22.     VectorCopy (cent->currentState.origin, cent->lerpOrigin);
  23.     VectorCopy (cent->currentState.angles, cent->lerpAngles);
  24.     if ( cent->currentState.eType == ET_PLAYER ) {
  25.         CG_ResetPlayerEntity( cent );
  26.     }
  27. }
  28.  
  29. /*
  30. ===============
  31. CG_TransitionEntity
  32.  
  33. cent->nextState is moved to cent->currentState and events are fired
  34. ===============
  35. */
  36. static void CG_TransitionEntity( centity_t *cent ) {
  37.     cent->currentState = cent->nextState;
  38.     cent->currentValid = qtrue;
  39.  
  40.     // reset if the entity wasn't in the last frame or was teleported
  41.     if ( !cent->interpolate ) {
  42.         CG_ResetEntity( cent );
  43.     }
  44.  
  45.     // clear the next state.  if will be set by the next CG_SetNextSnap
  46.     cent->interpolate = qfalse;
  47.  
  48.     // check for events
  49.     CG_CheckEvents( cent );
  50. }
  51.  
  52.  
  53. /*
  54. ==================
  55. CG_SetInitialSnapshot
  56.  
  57. This will only happen on the very first snapshot, or
  58. on tourney restarts.  All other times will use 
  59. CG_TransitionSnapshot instead.
  60.  
  61. FIXME: Also called by map_restart?
  62. ==================
  63. */
  64. void CG_SetInitialSnapshot( snapshot_t *snap ) {
  65.     int                i;
  66.     centity_t        *cent;
  67.     entityState_t    *state;
  68.  
  69.     cg.snap = snap;
  70.  
  71.     BG_PlayerStateToEntityState( &snap->ps, &cg_entities[ snap->ps.clientNum ].currentState, qfalse );
  72.  
  73.     // sort out solid entities
  74.     CG_BuildSolidList();
  75.  
  76.     CG_ExecuteNewServerCommands( snap->serverCommandSequence );
  77.  
  78.     // set our local weapon selection pointer to
  79.     // what the server has indicated the current weapon is
  80.     CG_Respawn();
  81.  
  82.     for ( i = 0 ; i < cg.snap->numEntities ; i++ ) {
  83.         state = &cg.snap->entities[ i ];
  84.         cent = &cg_entities[ state->number ];
  85.  
  86.         cent->currentState = *state;
  87.         cent->interpolate = qfalse;
  88.         cent->currentValid = qtrue;
  89.  
  90.         CG_ResetEntity( cent );
  91.  
  92.         // check for events
  93.         CG_CheckEvents( cent );
  94.     }
  95. }
  96.  
  97.  
  98. /*
  99. ===================
  100. CG_TransitionSnapshot
  101.  
  102. The transition point from snap to nextSnap has passed
  103. ===================
  104. */
  105. static void CG_TransitionSnapshot( void ) {
  106.     centity_t            *cent;
  107.     snapshot_t            *oldFrame;
  108.     int                    i;
  109.  
  110.     if ( !cg.snap ) {
  111.         CG_Error( "CG_TransitionSnapshot: NULL cg.snap" );
  112.     }
  113.     if ( !cg.nextSnap ) {
  114.         CG_Error( "CG_TransitionSnapshot: NULL cg.nextSnap" );
  115.     }
  116.  
  117.     // execute any server string commands before transitioning entities
  118.     CG_ExecuteNewServerCommands( cg.nextSnap->serverCommandSequence );
  119.  
  120.     // if we had a map_restart, set everthing with initial
  121.     if ( !cg.snap ) {
  122.     }
  123.  
  124.     // clear the currentValid flag for all entities in the existing snapshot
  125.     for ( i = 0 ; i < cg.snap->numEntities ; i++ ) {
  126.         cent = &cg_entities[ cg.snap->entities[ i ].number ];
  127.         cent->currentValid = qfalse;
  128.     }
  129.  
  130.     // move nextSnap to snap and do the transitions
  131.     oldFrame = cg.snap;
  132.     cg.snap = cg.nextSnap;
  133.  
  134.     BG_PlayerStateToEntityState( &cg.snap->ps, &cg_entities[ cg.snap->ps.clientNum ].currentState, qfalse );
  135.     cg_entities[ cg.snap->ps.clientNum ].interpolate = qfalse;
  136.  
  137.     for ( i = 0 ; i < cg.snap->numEntities ; i++ ) {
  138.         cent = &cg_entities[ cg.snap->entities[ i ].number ];
  139.         CG_TransitionEntity( cent );
  140.     }
  141.  
  142.     cg.nextSnap = NULL;
  143.  
  144.     // check for playerstate transition events
  145.     if ( oldFrame ) {
  146.         playerState_t    *ops, *ps;
  147.  
  148.         ops = &oldFrame->ps;
  149.         ps = &cg.snap->ps;
  150.         // teleporting checks are irrespective of prediction
  151.         if ( ( ps->eFlags ^ ops->eFlags ) & EF_TELEPORT_BIT ) {
  152.             cg.thisFrameTeleport = qtrue;    // will be cleared by prediction code
  153.         }
  154.  
  155.         // if we are not doing client side movement prediction for any
  156.         // reason, then the client events and view changes will be issued now
  157.         if ( cg.demoPlayback || (cg.snap->ps.pm_flags & PMF_FOLLOW)
  158.             || cg_nopredict.integer || cg_syncronousClients.integer ) {
  159.             CG_TransitionPlayerState( ps, ops );
  160.         }
  161.  
  162.     }
  163.  
  164. }
  165.  
  166.  
  167. /*
  168. ===================
  169. CG_SetNextSnap
  170.  
  171. A new snapshot has just been read in from the client system.
  172. ===================
  173. */
  174. static void CG_SetNextSnap( snapshot_t *snap ) {
  175.     int                    num;
  176.     entityState_t        *es;
  177.     centity_t            *cent;
  178.  
  179.     cg.nextSnap = snap;
  180.  
  181.     BG_PlayerStateToEntityState( &snap->ps, &cg_entities[ snap->ps.clientNum ].nextState, qfalse );
  182.     cg_entities[ cg.snap->ps.clientNum ].interpolate = qtrue;
  183.  
  184.     // check for extrapolation errors
  185.     for ( num = 0 ; num < snap->numEntities ; num++ ) {
  186.         es = &snap->entities[num];
  187.         cent = &cg_entities[ es->number ];
  188.  
  189.         cent->nextState = *es;
  190.  
  191.         // if this frame is a teleport, or the entity wasn't in the
  192.         // previous frame, don't interpolate
  193.         if ( !cent->currentValid || ( ( cent->currentState.eFlags ^ es->eFlags ) & EF_TELEPORT_BIT )  ) {
  194.             cent->interpolate = qfalse;
  195.         } else {
  196.             cent->interpolate = qtrue;
  197.         }
  198.     }
  199.  
  200.     // if the next frame is a teleport for the playerstate, we
  201.     // can't interpolate during demos
  202.     if ( cg.snap && ( ( snap->ps.eFlags ^ cg.snap->ps.eFlags ) & EF_TELEPORT_BIT ) ) {
  203.         cg.nextFrameTeleport = qtrue;
  204.     } else {
  205.         cg.nextFrameTeleport = qfalse;
  206.     }
  207.  
  208.     // if changing follow mode, don't interpolate
  209.     if ( cg.nextSnap->ps.clientNum != cg.snap->ps.clientNum ) {
  210.         cg.nextFrameTeleport = qtrue;
  211.     }
  212.  
  213.     // if changing server restarts, don't interpolate
  214.     if ( ( cg.nextSnap->snapFlags ^ cg.snap->snapFlags ) & SNAPFLAG_SERVERCOUNT ) {
  215.         cg.nextFrameTeleport = qtrue;
  216.     }
  217.  
  218.     // sort out solid entities
  219.     CG_BuildSolidList();
  220. }
  221.  
  222.  
  223. /*
  224. ========================
  225. CG_ReadNextSnapshot
  226.  
  227. This is the only place new snapshots are requested
  228. This may increment cgs.processedSnapshotNum multiple
  229. times if the client system fails to return a
  230. valid snapshot.
  231. ========================
  232. */
  233. static snapshot_t *CG_ReadNextSnapshot( void ) {
  234.     qboolean    r;
  235.     snapshot_t    *dest;
  236.  
  237.     if ( cg.latestSnapshotNum > cgs.processedSnapshotNum + 1000 ) {
  238.         CG_Error( "CG_ReadNextSnapshot: way out of range, %i > %i", 
  239.             cg.latestSnapshotNum, cgs.processedSnapshotNum );
  240.     }
  241.  
  242.     while ( cgs.processedSnapshotNum < cg.latestSnapshotNum ) {
  243.         // decide which of the two slots to load it into
  244.         if ( cg.snap == &cg.activeSnapshots[0] ) {
  245.             dest = &cg.activeSnapshots[1];
  246.         } else {
  247.             dest = &cg.activeSnapshots[0];
  248.         }
  249.  
  250.         // try to read the snapshot from the client system
  251.         cgs.processedSnapshotNum++;
  252.         r = trap_GetSnapshot( cgs.processedSnapshotNum, dest );
  253.  
  254.         // if it succeeded, return
  255.         if ( r ) {
  256.             CG_AddLagometerSnapshotInfo( dest );
  257.             return dest;
  258.         }
  259.  
  260.         // a GetSnapshot will return failure if the snapshot
  261.         // never arrived, or  is so old that its entities
  262.         // have been shoved off the end of the circular
  263.         // buffer in the client system.
  264.  
  265.         // record as a dropped packet
  266.         CG_AddLagometerSnapshotInfo( NULL );
  267.  
  268.         // If there are additional snapshots, continue trying to
  269.         // read them.
  270.     }
  271.  
  272.     // nothing left to read
  273.     return NULL;
  274. }
  275.  
  276.  
  277. /*
  278. ============
  279. CG_ProcessSnapshots
  280.  
  281. We are trying to set up a renderable view, so determine
  282. what the simulated time is, and try to get snapshots
  283. both before and after that time if available.
  284.  
  285. If we don't have a valid cg.snap after exiting this function,
  286. then a 3D game view cannot be rendered.  This should only happen
  287. right after the initial connection.  After cg.snap has been valid
  288. once, it will never turn invalid.
  289.  
  290. Even if cg.snap is valid, cg.nextSnap may not be, if the snapshot
  291. hasn't arrived yet (it becomes an extrapolating situation instead
  292. of an interpolating one)
  293.  
  294. ============
  295. */
  296. void CG_ProcessSnapshots( void ) {
  297.     snapshot_t        *snap;
  298.     int                n;
  299.  
  300.     // see what the latest snapshot the client system has is
  301.     trap_GetCurrentSnapshotNumber( &n, &cg.latestSnapshotTime );
  302.     if ( n != cg.latestSnapshotNum ) {
  303.         if ( n < cg.latestSnapshotNum ) {
  304.             // this should never happen
  305.             CG_Error( "CG_ProcessSnapshots: n < cg.latestSnapshotNum" );
  306.         }
  307.         cg.latestSnapshotNum = n;
  308.     }
  309.  
  310.     // If we have yet to receive a snapshot, check for it.
  311.     // Once we have gotten the first snapshot, cg.snap will
  312.     // always have valid data for the rest of the game
  313.     while ( !cg.snap ) {
  314.         snap = CG_ReadNextSnapshot();
  315.         if ( !snap ) {
  316.             // we can't continue until we get a snapshot
  317.             return;
  318.         }
  319.  
  320.         // set our weapon selection to what
  321.         // the playerstate is currently using
  322.         if ( !( snap->snapFlags & SNAPFLAG_NOT_ACTIVE ) ) {
  323.             CG_SetInitialSnapshot( snap );
  324.         }
  325.     }
  326.  
  327.     // loop until we either have a valid nextSnap with a serverTime
  328.     // greater than cg.time to interpolate towards, or we run
  329.     // out of available snapshots
  330.     do {
  331.         // if we don't have a nextframe, try and read a new one in
  332.         if ( !cg.nextSnap ) {
  333.             snap = CG_ReadNextSnapshot();
  334.  
  335.             // if we still don't have a nextframe, we will just have to
  336.             // extrapolate
  337.             if ( !snap ) {
  338.                 break;
  339.             }
  340.  
  341.             CG_SetNextSnap( snap );
  342.  
  343.             // if time went backwards, we have a level restart
  344.             if ( cg.nextSnap->serverTime < cg.snap->serverTime ) {
  345.                 CG_Error( "CG_ProcessSnapshots: Server time went backwards" );
  346.             }
  347.         }
  348.  
  349.         // if our time is < nextFrame's, we have a nice interpolating state
  350.         if ( cg.time >= cg.snap->serverTime && cg.time < cg.nextSnap->serverTime ) {
  351.             break;
  352.         }
  353.  
  354.         // we have passed the transition from nextFrame to frame
  355.         CG_TransitionSnapshot();
  356.     } while ( 1 );
  357.  
  358.     // assert our valid conditions upon exiting
  359.     if ( cg.snap == NULL ) {
  360.         CG_Error( "CG_ProcessSnapshots: cg.snap == NULL" );
  361.     }
  362.     if ( cg.time < cg.snap->serverTime ) {
  363.         // this can happen right after a vid_restart
  364.         cg.time = cg.snap->serverTime;
  365.     }
  366.     if ( cg.nextSnap != NULL && cg.nextSnap->serverTime <= cg.time ) {
  367.         CG_Error( "CG_ProcessSnapshots: cg.nextSnap->serverTime <= cg.time" );
  368.     }
  369.  
  370. }
  371.  
  372.